package top.saplf.sileny.ext

import android.animation.ValueAnimator
import android.app.Activity
import android.content.Context
import android.view.View
import android.view.WindowManager

/**
 * @author saplf
 */

/**
 * 向下滑动隐藏/显现的动画
 * @param show 是显现还是隐藏
 * @param end 动画完成之后的动作
 */
fun View.slideDownAppear(show: Boolean, end: (() -> Unit)? = null) {
    postOnAnimation {
        if (show && alpha == 0f) {
            alpha = 1f
        }
        with(ValueAnimator.ofFloat().apply { duration = 120 }) {
            removeAllListeners()
            removeAllUpdateListeners()
            addUpdateListener { translationY = it.animatedValue as Float }
            addListenerStopListener { end?.invoke() }
            if (show) {
                setFloatValues(height.toFloat(), 0f)
            } else {
                setFloatValues(0f, height.toFloat())
            }
            start()
        }
    }
}

/**
 * 某[Activity]的状态栏是否透明，需要在 setContentView 之后调用
 * @param translucent 是否透明
 */
fun Activity.statusBarTranslucent(translucent: Boolean) {
    val attributes = window.attributes
    if (translucent) {
        attributes.flags = attributes.flags or WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
    } else {
        attributes.flags = attributes.flags and WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS.inv()
    }
}
